import recharge from '@/api/recharge'
import pay from '@/api/pay'

const state = {
    phone: '',
    location: '',
    requestSended: false,
    goods: {
        type1: {
            name: '共享流量',
            tip: '仅限浙江移动号码办理，流量全国范围通用，当月有效，每个号码每月只能办理一次。',
            sales: 0,
            score: '0%',
            content: {
                thisMonth: [],
                nextMonth: []
            }
        },
        type2: {
            name: '流量红包',
            tip: '仅限浙江移动号码办理，流量全国范围内通用，当月有效，每个号码不限次。',
            sales: 0,
            score: '0%',
            content: []
        },
        type3: {
            name: '预存话费',
            tip: '全国范围内通用。话费分月返还，非一次性到账。',
            sales: 0,
            score: '100%',
            content: {
                duration1: [],
                duration2: [],
            }
        },
        type4: {
            name: '话费直充',
            tip: '全国范围内通用，话费一次性到账。',
            sales: 0,
            score: '100%',
            content: []
        }
    }
}

const getters = {
    phone: state => state.phone,
    location: state => state.location,
    goods: state => state.goods
}

const actions = {
    async savePhone ({commit}, phone) {
        commit('setPhone', phone)
        if (phone.length < 11) {
            commit('setLocation', '')
        } else if (/^1[34578]\d{9}$/.test(phone)) {
            const d = await recharge.getLocation(phone)
            const res = { data: d.data }
            const data = (new Function(res.data + ';return __GetZoneResult_'))()
            commit('setLocation', data.carrier)
        }
    },
    async getGoods ({state, commit}) {
        if (state.requestSended) return
        const r = await recharge.getGoods()
        commit('setGoods', r.data)
    },
    async submitOrder ({state, rootState, commit}, {phone, goodsId, datangNo}) {
        const r = await recharge.submitOrder({
            userId: rootState.user.userInfo.user_id,
            phone,
            goodsId,
            datangNo
        })
        return r
    },
    async pay({state, rootState}, { orderId, price, successCb, failCb }) {
        const { success, data, msg } = await pay.getCharge({
            type: 4,
            orderId,
            userId: rootState.user.userInfo.user_id
        })
        if (!success) return {success, msg}
        const charge = data.charge
        try {
            await pay.WxPay(charge)
            // alert('回调成功')
            successCb()
        } catch (err) {
            // alert('回调失败')
            failCb()
        }
        return { success: true }
    }
}

const mutations = {
    setGoods (state, data) {
        state.requestSended = true
        // 共享流量 on_time==1代表本月
        // 暂时对销量未做区分
        state.goods.type1.sales = data.t1.t11
        state.goods.type1.content = {
            thisMonth: data.type1.filter(t => t.on_time == 1),
            nextMonth: data.type1.filter(t => t.on_time != 1)
        }
        // 流量红包
        state.goods.type2.sales = data.t2
        state.goods.type2.content = data.type2
         // 预存话费 duration == 1代表6个月充值
         state.goods.type3.sales = data.t3
         state.goods.type3.content = {
             duration1: data.type3.filter(t => t.duration == 1),
             duration2: data.type3.filter(t => t.duration != 1)
         }
        // 话费直充
        state.goods.type4.sales = data.t4
        state.goods.type4.content = data.type4
    },
    setPhone (state, phone) {
        state.phone = phone
    },
    setLocation (state, location) {
        state.location = location
    }
}

export default{
    namespaced: true,
    state,
    getters,
    actions,
    mutations
}
